package com.nyt.org.modules.LockSeat.service;

import com.alibaba.fastjson.JSONObject;
import com.nyt.org.common.utils.SendPostRequest;
import com.nyt.org.common.utils.xml.XmlUtils;
import com.nyt.org.modules.LockSeat.model.REQ2005;
import com.nyt.org.modules.LockSeat.model.REQBODY2005;
import com.nyt.org.modules.LockSeat.model.REQROOT2005;
import com.nyt.org.modules.LockSeat.model.RSPROOT2005;
import com.nyt.org.modules.TicketStaion.service.FaService;
import com.nyt.org.modules.api.constant.ZhongLvCode;
import com.nyt.org.modules.api.model.XMLHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoctSeatDoService extends FaService {

    private Logger logger = LoggerFactory.getLogger(LoctSeatDoService.class);

    @Value("${zhonglv.server}")
    private String zhonglvURL;

    public Map<String,Object> lockSeat(JSONObject bodyJson){
        Map<String,Object> map = new HashMap<>();

        REQROOT2005 root = new REQROOT2005();

        XMLHead head = super.bulldHEADXml(ZhongLvCode.下单锁座接口);
        root.setHead(head);

        REQBODY2005 body = new REQBODY2005();
        REQ2005 req2005 = new REQ2005();
        req2005.setBusNo(bodyJson.getString("BusNo"));
        req2005.setTicketNumStr(bodyJson.getString("TicketNumStr"));
        req2005.setDepartureCityNo(bodyJson.getString("DepartureCityNo"));
        req2005.setArrivalCityNo(bodyJson.getString("ArrivalCityNo"));
        req2005.setOnStationNo(bodyJson.getString("OnStationNo"));
        req2005.setOffStationNo(bodyJson.getString("OffStationNo"));
        req2005.setDepartureDate(bodyJson.getString("DepartureDate"));
        req2005.setCurrency(bodyJson.getString("Currency"));
        req2005.setOrgNo(bodyJson.getString("OrgNo"));
        req2005.setPreferentialAmount(bodyJson.getString("PreferentialAmount"));
        req2005.setUserNickName(bodyJson.getString("UserNickName"));
        req2005.setUserTelNo(bodyJson.getString("UserTelNo"));
        req2005.setStaffCardNo(bodyJson.getString("StaffCardNo"));
        req2005.setCouponNo(bodyJson.getString("CouponNo"));
        req2005.setPassengerPaperJson(bodyJson.getString("PassengerPaperJson"));
        req2005.setExtOrderNo(bodyJson.getString("ExtOrderNo"));
        req2005.setLinkOriginCode(bodyJson.getString("LinkOriginCode"));
        req2005.setUseCarrierCode(bodyJson.getString("UseCarrierCode"));
        body.setReq2005(req2005);

        root.setBody(body);

        String reqXml = XmlUtils.toXml(root);
        logger.info("下单锁座-接口[2005]请求报文：\n"+reqXml);

        String respXml = SendPostRequest.doPost(zhonglvURL,reqXml);
        logger.info("下单锁座-接口[2005]响应报文：\n"+respXml);

        RSPROOT2005 rsproot2005 = XmlUtils.xmlToBean(respXml, RSPROOT2005.class);

        map.put("rspCode",rsproot2005.getHead().getRspCode());
        map.put("rspMsg",rsproot2005.getHead().getRspMsg());
        map.put("responseBean",rsproot2005);

        return map;
    }


}
